package com.csscis.ssm.web.controller;


import org.apache.ibatis.annotations.Param;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import com.csscis.ssm.core.feature.orm.mybatis.Page;
import com.csscis.ssm.core.util.JsonUtils;
import com.csscis.ssm.web.model.MessageTable;
import com.csscis.ssm.web.service.MessageTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/information")
public class MessageContorller {
    @Autowired
    private MessageTableService messageTableService;


    @RequestMapping("/start")
    public String start(){
        return "messageList";
    }

    /**
     * 查询所有
     * @throws IOException
     */
    @RequestMapping("/messageList")
    @ResponseBody
    public String List(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
            MessageTable messageTable,
            String finde) throws IOException {

        String result = null;
        if (finde != null && "".equals(finde)) {
            if ("".equals(messageTable.getMessageId())) {
                messageTable.setMessageId(null);
            }
        }
        try {
            Page<MessageTable> page = new Page<MessageTable>(pageNo, pageSize);
            messageTableService.selectAndPage(page, messageTable);
            result = JsonUtils.jsonResult(page.getResult(), page.getTotalCount());
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 进入添加
     * @param request
     * @return
     */
    @RequestMapping("/doAddMessage")
    public String doAddMessageServer(HttpServletRequest request) {

        request.setAttribute("type", "add");
        return "messageEdit";
    }

    /**
     * 进入修改
     * @param request
     * @return
     */
    @RequestMapping("/messageById")
    public String PhoneUserById(Long id, HttpServletRequest request) {
        MessageTable messageTable = messageTableService.selectById(id);
        request.setAttribute("type", "update");
        request.setAttribute("messageTable", messageTable);
        return "messageEdit";
    }

    /**
     * 增加操作
     * @param
     * @return
     */
    @RequestMapping("/messageAdd")
    public String MessageAdd(MessageTable messageTable, HttpServletRequest request) {
        try {
            messageTableService.insert(messageTable, request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "messageList";
    }

    /**
     * 修改操作
     * @param
     * @return
     */
    @RequestMapping("/messageUpload")
    public String MessageUpload(MessageTable messageTable) {
        messageTableService.update(messageTable);
        return "phoneUserList";
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @RequestMapping("/messageDel")
    public String MessageDel(String id) {

        String[] ids = id.split(",");
        if (ids.length != 0) {
            for (String s : ids) {
                messageTableService.delete(Long.valueOf(s));
            }
        }
        return "messageList";
    }

    /**
     * 名称唯一性验证
     * @param model 对象
     * @param response
     */
    @RequestMapping(value = "/messageNameOnly")
    public void selectByModel(@Param("id") Long id, MessageTable model, HttpServletResponse response) {

        //selectAll 方法要写相应的条件
        List<MessageTable> list = messageTableService.selectAll(model);
        List<String> lists = new ArrayList<String>();
        for (int i = 0; i < list.size(); i++) {
            MessageTable messageTable = list.get(i);
            if (messageTable.getMessageTableId() != id) {
                lists.add(i, messageTable.getMessageHead());
            }
        }
        JsonUtils.checkResult(lists, response);
    }
}
